package com.quectel.emp.controller.banner;

import cn.hutool.core.lang.func.LambdaUtil;
import com.quectel.core.module.banner.dto.BannerDto;
import com.quectel.core.module.banner.dto.MobileBannerVillageDto;
import com.quectel.core.module.banner.service.BannerService;
import com.quectel.core.module.village.dto.VillageDto;
import com.quectel.core.module.village.service.VillageService;
import com.quectel.emp.controller.BaseController;
import com.quectel.util.common.Page;
import com.quectel.util.common.Request;
import com.quectel.util.common.Response;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.apache.dubbo.config.annotation.DubboReference;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

/**
 * @author bob
 * @email bob.yu@quectel.com
 * @date 2022/5/31 17:16
 */
@RestController
@RequestMapping("banner")
@Tag(name = "BannerController", description = "员工移动端banner相关api")
public class BannerController extends BaseController {


    private static final Logger LOGGER = LoggerFactory.getLogger(BannerController.class);

    @DubboReference
    private BannerService bannerService;
    @DubboReference
    private VillageService villageService;

    @GetMapping("list")
    @Operation(summary = "返回banner图列表,支持分页查询")
    public Response<Page<BannerDto>> list(
            @Parameter(description = "villageCode") @RequestParam(required = true) String villageCode,
            @Parameter(description = "资源类型 1.首页banner 2.广告") @RequestParam(required = true) String bannerType,
            @Parameter(description = "使用位置 1.顶部 2.中部 3.底部") @RequestParam(required = true) String useType) {
        Request request = Request.configParams(Request.parseParams(httpServletRequest));
        VillageDto villageDto = villageService.selectByCode(villageCode);
        if (villageDto == null) {
            return Response.error("社区不存在");
        }
        request.put(LambdaUtil.getFieldName(MobileBannerVillageDto::getVillageId), villageDto.getId());
        request.put(LambdaUtil.getFieldName(BannerDto::getTenantId), villageDto.getTenantId());
        request.put(LambdaUtil.getFieldName(BannerDto::getBannerType), bannerType);
        request.put(LambdaUtil.getFieldName(BannerDto::getUseType), useType);
        int totalCount = bannerService.queryTotal(request.getParams());
        List<BannerDto> list = null;
        if (totalCount > 0) {
            list = bannerService.queryList(request.getParams());

        } else {
            list = new ArrayList<>();
        }
        Page<BannerDto> page = new Page<>(request.getPageNo(), request.getPageSize(), totalCount, list);
        return Response.<Page<BannerDto>>ok().wrap(page);
    }

}